# -*- coding: utf-8 -*-
# standard
import os
import sys
from json import JSONDecodeError

# third

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from models import *



"""
@Title:   
@File: 从数据库导出数据.py
@Author: walle 2023年12日11日 12时40分23秒
@Version: 1.0.0
@Desc: 
"""


JSON_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "export_records1.json")


def export_downloaded_files_to_json():
    """
    把已下载过的文件导出为json
    """
    db_session = new_db_session()
    cond = or_(
        MediaFileOrm.remote_storage != "",
        MediaFileOrm.error != "",
        MediaFileOrm.save_path != ""
    )
    records = {}
    keys = []
    for one in db_session.query(MediaFileOrm.key).filter(and_(MediaFileOrm.key != 'kBvAgHyLozo', cond)):
        key = one[0]
        x = None
        try:
            x = db_session.query(MediaFileOrm).filter(MediaFileOrm.key == key).first()
        except JSONDecodeError as e:
            print(key)
            keys.append(key)
        except Exception as e:
            raise e
        finally:
            if not x:
                pass
            else:
                current = {
                    # "key": x.key,
                    "caption": x.caption,
                    "source_site": x.source_site,
                    "page_url": x.page_url,
                    "resource": MediaFileOrm.reduce_resource(x.resource, x.key) if len(x.resource) > 0 else x.resource,
                    "error": x.error,
                    "remote_storage": x.remote_storage,
                    "save_path": x.save_path,
                    "parse_time": x.parse_time.strftime("%Y-%m-%d %H:%M:%S.%f") if x.parse_time else x.parse_time,
                    "download_begin": x.download_begin.strftime("%Y-%m-%d %H:%M:%S.%f") if x.download_begin else x.download_begin,
                    "download_end": x.download_end.strftime("%Y-%m-%d %H:%M:%S.%f") if x.download_end else x.download_end,
                } 
                
                records[x.key] = current
    with open(JSON_PATH, "w", encoding="utf-8") as f:
        json.dump(records, f, ensure_ascii=False, indent=4) 
    print(JSON_PATH)


def reduce_resource_in_db():
    """
    化简数据库里的全部 resource 字段，避免超出json的8k限制
    """
    db_session = new_db_session()
    num = 0
    keys = []
    for x in db_session.query(MediaFileOrm.key).filter(MediaFileOrm.download_begin != None):
        key = x.key
        one = None
        try:
            one = db_session.query(MediaFileOrm).filter(MediaFileOrm.key == key).one()
        except JSONDecodeError as e:
            print(key)
            keys.append(key)
        except Exception as e:
            raise e
        finally:
            if one:
                print(f"key {one.create_time} {one.download_begin} {one.download_end}")
        # try:
            
        #     one = db_session.query(MediaFileOrm).filter(MediaFileOrm.key == key).one()
        #     if len(one.resource) > 0:
        #         if "caption" in one.resource:  # 已处理过的
        #             continue
        #         print(f"{key} = {one.to_dict()}")
        #         one.resource = MediaFileOrm.reduce_resource(one.resource)
        #         print(f"{key} os commit")
        #         db_session.commit()
        #         num += 1
        #         print(num)
        #     else:
        #         pass
            
        # except JSONDecodeError as e:
        #     print(key)
        # except Exception as e:
        #     raise e
    db_session.close()


def import_downloaded_files_from_json():
    """
    从json文件导入数据
    """
    with open(JSON_PATH, "r", encoding="utf-8") as f:
        records = json.load(f)
    db_session = new_db_session()
    for key, update_info in records.items():
        if update_info['parse_time']:
            update_info['parse_time'] = datetime.strptime(update_info['parse_time'], "%Y-%m-%d %H:%M:%S.%f")
        if update_info['download_begin']:
            update_info['download_begin'] = datetime.strptime(update_info['download_begin'], "%Y-%m-%d %H:%M:%S.%f")
        if update_info['download_end']:
            update_info['download_end'] = datetime.strptime(update_info['download_end'], "%Y-%m-%d %H:%M:%S.%f")
        db_session.query(MediaFileOrm).filter(MediaFileOrm.key == key).update(update_info)
    db_session.commit()
    db_session.close()



if __name__ == '__main__':
    # reduce_resource_in_db()
    # export_downloaded_files_to_json()
    # import_downloaded_files_from_json()
    pass